#include"ds1302.h"
void DS1302_write_byte(unsigned char date)
{
	unsigned char k;
	for(k=0;k<8;k++)
	{
		DS1302_CLK=0;
		DS1302_IO=date&0x01;
		date>>=1;
		DS1302_CLK=1;		
	}
}
unsigned char DS1302_read_byte(void)
{
	unsigned char k,date;
	for(k=0;k<8;k++)
	{
		date>>=1;
		DS1302_CLK=1;
		DS1302_CLK=0;
		if(DS1302_IO)
			date|=0x80;				
	}
	return date;
}
void DS1302_write(unsigned char add,unsigned char date)
{
	DS1302_CE=0;
	DS1302_CLK=0;
	DS1302_CE=1;
	
	DS1302_write_byte(add);
	DS1302_write_byte(date);

	DS1302_CE=0;
	DS1302_CLK=0;
}
unsigned char DS1302_read(unsigned char add)
{
	unsigned char date;
	DS1302_CE=0;
	DS1302_CLK=0;
	DS1302_CE=1;

	DS1302_write_byte(add);
	date=DS1302_read_byte();

	DS1302_CE=0;
	DS1302_CE=1;

	DS1302_write_byte(add);
	DS1302_read_byte();

	DS1302_CE=0;
	DS1302_CLK=0;

	return date;
}
void DS1302_init(void)
{
	DS1302_write(0X8E,0X00);

	DS1302_write(YEARW,0X16);
	DS1302_write(MONTHW,0X05);
	DS1302_write(DATEW,0X14);
	DS1302_write(DAYW,0X06);
	DS1302_write(HOURW,0X12);
	DS1302_write(MINW,0X30);
	DS1302_write(SECW,0X00);

	DS1302_write(0X8E,0X80);		
}